package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.sellerquery2.parameter.GetOrderTradeflowBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.entities.OrderTradeflow;
import com.ymatou.iapi.trading.sellerquery2.parameter.entities.OrderTradeflowRefund;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetOrderTradeflowResp;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;
/**
*获取订单退货退款信息
*FreeMaker Template自动生成代码
*/
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetOrderTradeflow {
	private static GetOrderTradeflowBean getordertradeflowBean;
	//	private static com.ymatou.query.seller.trading.api.service.GetOrderTradeflowCall getordertradeflowCall;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetOrderTradeflowCall getordertradeflowCall;

	private static OrderTradeflow trade;
	private static List<OrderTradeflowRefund> refunds;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取单个订单信息");
	}

	@Before
	public void caseUp() {
		getordertradeflowBean = new GetOrderTradeflowBean();
		//		getordertradeflowCall=new com.ymatou.query.seller.trading.api.service.GetOrderTradeflowCall();
		getordertradeflowCall = new com.ymatou.iapi.trading.sellerquery2.service.GetOrderTradeflowCall();
		trade = new OrderTradeflow();
		refunds = new ArrayList<OrderTradeflowRefund>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getordertradeflowCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetOrderTradeflowResp expectResponse = new GetOrderTradeflowResp();
		ConstructExpectedResponse(expectResponse);
		GetOrderTradeflowResp actualResponse = getordertradeflowCall
				.getOrderTradeflowResp();
		OrderTradeflow expect = expectResponse.getOrderTradeflow();
		OrderTradeflow actual = actualResponse.getOrderTradeflow();
		if (expect == null) {
			DataUtil.verify(expect, actual, "验证OrderTradeflow=null");
		} else {
			boolean goon = actual != null;
			DataUtil.verify(true, goon, "验证OrderTradeflow != null");
			if (goon) {
				DataUtil.verifyDateEquals(expect.getCancelTime(),
						actual.getCancelTime(), "验证CancelTime");
				DataUtil.verify(expect.getDiscount(), actual.getDiscount(),
						"验证Discount");
				DataUtil.verify(expect.getDiscountOfThirdParty(),
						actual.getDiscountOfThirdParty(),
						"验证DiscountOfThirdParty");
				DataUtil.verify(expect.getFreight(), actual.getFreight(),
						"验证Freight");
				DataUtil.verify(expect.getOrderId(), actual.getOrderId(),
						"验证OrderId");
				DataUtil.verify(expect.getPaidAmountOfCash(),
						actual.getPaidAmountOfCash(), "验证PaidAmountOfCash");
				DataUtil.verify(expect.getPaidAmountOfGift(),
						actual.getPaidAmountOfGift(), "验证PaidAmountOfGift");
				DataUtil.verifyDateEquals(expect.getPaidTime(),
						actual.getPaidTime(), "验证PaidTime");
				DataUtil.verify(expect.getPayChannel(), actual.getPayChannel(),
						"验证PayChannel");
				DataUtil.verify(expect.getPostDiscount(),
						actual.getPostDiscount(), "验证PostDiscount");
				DataUtil.verify(expect.getPostPaidAmountOfCash(),
						actual.getPostPaidAmountOfCash(),
						"验证PostPaidAmountOfCash");
				DataUtil.verify(expect.getPostPaidAmountOfGift(),
						DataUtil.BigDecimall.GetValueOrDefault(actual
								.getPostPaidAmountOfGift()),
						"验证PostPaidAmountOfGift");
				DataUtil.verifyDateEquals(expect.getPostPaidTime(),
						actual.getPostPaidTime(), "验证PostPaidTime");
				DataUtil.verify(expect.getSellerCouponAmount(),
						actual.getSellerCouponAmount(), "验证SellerCouponAmount");
				DataUtil.verify(expect.getSellerCouponCode(),
						actual.getSellerCouponCode(), "验证SellerCouponCode");
				DataUtil.verify(expect.getTotalPrice(), actual.getTotalPrice(),
						"验证TotalPrice");
				DataUtil.verify(expect.getTradingStatus(),
						actual.getTradingStatus(), "验证TradingStatus");
				DataUtil.verify(expect.getYmtCouponAmount(),
						actual.getYmtCouponAmount(), "验证YmtCouponAmount");
				DataUtil.verify(expect.getYmtCouponCode(),
						actual.getYmtCouponCode(), "验证YmtCouponCode");
				List<OrderTradeflowRefund> exps = expect
						.getOrderTradeflowRefundList();
				List<OrderTradeflowRefund> acts = actual
						.getOrderTradeflowRefundList();
				if (DataUtil.isNullOrEmpty(exps)) {
					DataUtil.verify(true, DataUtil.isNullOrEmpty(acts),
							"验证OrderTradeflowRefundList==null或者空");
				} else {
					boolean process = DataUtil.isNullOrEmpty(acts) == false;
					DataUtil.verify(true, process,
							"验证OrderTradeflowRefundList!=null且size()>0");
					if (process) {
						boolean sameSize = exps.size() == acts.size();
						DataUtil.verify(exps.size(), acts.size(),
								"验证OrderTradeflowRefundList.size()");
						if (sameSize) {
							for (OrderTradeflowRefund exp : exps) {
								DataUtil.verify(
										true,
										acts.stream().anyMatch(
												x -> x.toString().equals(
														exp.toString())),
										"验证\n"
												+ exp.toString()
												+ "\n在OrderTradeflowRefundList中存在");
							}
						}
					}
				}
			}
		}
	}

	private void ConstructExpectedResponse(GetOrderTradeflowResp response) {
		if (getordertradeflowBean.getOrderId() != 0) {
			execute(getordertradeflowBean.getOrderId());
			response.setOrderTradeflow(trade);
		}
	}

	@SuppressWarnings("static-access")
	private void execute(int orderId) {
		DefaultWapper db = new DefaultWapper();
		List<Map> trade = db.executeSql(getOrderTradeflow1(orderId));
		if (!DataUtil.isNullOrEmpty(trade)) {
			DataUtil.mapToBean(this.trade, trade.get(0));
			List<Map> refunds = db.executeSql(getOrderTradeflow2(orderId));
			if (false == DataUtil.isNullOrEmpty(refunds)) {
				for (Map m : refunds) {
					OrderTradeflowRefund refund = new OrderTradeflowRefund();
					DataUtil.mapToBean(refund, m);
					this.refunds.add(refund);
				}
				this.trade.setOrderTradeflowRefundList(this.refunds);
			} else {
				this.trade.setOrderTradeflowRefundList(null);
			}
		} else {
			this.trade = null;
		}
	}

	public static String getOrderTradeflow1(int orderId) {
		return "SELECT"
				+ " a.iOrderId AS orderId,"
				+ " a.dPaidTime AS paidTime,"
				+ " a.dPostPaidTime AS postPaidTime,"
				+ "  a.dCancelTime AS cancelTime,"
				+ " a.sYmtCouponCode AS ymtCouponCode,"
				+ " a.fYmtCouponAmount AS ymtCouponAmount,"
				+ " a.sSellerCouponCode AS sellerCouponCode,"
				+ "  a.fSellerCouponAmount AS sellerCouponAmount,"
				+ "  a.fFreight AS freight,"
				+ "  a.fOrderDiscount AS discount,"
				+ "  a.fDiscount AS postDiscount,"
				+ "  a.fTotalPrice AS totalPrice,"
				+ "  a.iTradingStatus AS tradingStatus,"
				+ " b.sPayChannel AS payChannel,"
				+ " c.fPaidAmountOfCash AS paidAmountOfCash,"
				+ " c.fPaidAmountOfGift AS paidAmountOfGift,"
				+ " c.fPostPaidAmountOfCash AS postPaidAmountOfCash,"
				+ " c.fPostPaidAmountOfGift AS postPaidAmountOfGift,"
				+ " c.fDiscountOfThirdParty as discountOfThirdParty"
				+ " FROM"
				+ "	YmtRelease.dbo.Ymt_Orders a (NOLOCK)"
				+ " LEFT JOIN YmtRelease.dbo.Ymt_TradingInfo b (NOLOCK) ON a.iTradingId = b.iTradingId"
				+ " LEFT JOIN YmtRelease.dbo.Ymt_OrderState c (NOLOCK) ON a.iOrderId = c.iOrderId"
				+ " WHERE" + "	a.iOrderId = " + orderId;
	}

	public static String getOrderTradeflow2(int orderId) {
		return "SELECT"
				+ " 	a.OrderId as orderId,"
				+ " 	a.AddTime as addTime,"
				+ " 	a.RefundBillNo as refundBillNo,"
				+ " 	a.RefundAmount as refundAmount,"
				+ " 	b.ProcessResult AS refundChannel"
				+ " FROM"
				+ " 	YmtRelease.dbo.Ymt_RefundBill a (NOLOCK)"
				+ " LEFT JOIN YmtRelease.dbo.Ymt_SettlementInvoice b (NOLOCK) ON a.OrderId = b.OrderId"
				+ " AND b.BusinessType = 2"
				+ " AND a.RefundBillNo = b.BusinessNo" + " WHERE"
				+ " 	a.OrderId = " + orderId + " AND a.SalesRefundStatus = 10;";
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetOrderTradeflow_001() {
		Logger.start(true, "该订单有退款记录,获取单个订单信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetOrderTradeflow_002() {
		Logger.start(true, "获取单个订单信息-下单状态成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetOrderTradeflow_003() {
		Logger.start(true, "订单号不存在,获取单个订单信息成功");
		try {
			int orderId = 404;

			String param = "OrderId=" + orderId;
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetOrderTradeflow_004() {
		Logger.start(true, "订单号为null,获取单个订单信息成功");
		try {

			String param = "";
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getordertradeflowCall.getCode(), "验证code");
			Logger.verifyEquals(100, getordertradeflowCall.getBCode(),
					"验证bcode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_005() {
		Logger.start(true, "获取单个订单信息成功-已付款");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create2_PayOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_006() {
		Logger.start(true, "获取单个订单信息成功-已发货");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create3_ShippedOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_007() {
		Logger.start(true, "获取单个订单信息成功-已收货");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_ReceivedOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_008() {
		Logger.start(true, "获取单个订单信息成功-申请退款");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_ApplyRefundOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_009() {
		Logger.start(true, "获取单个订单信息成功-用户取消");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create12_UserCancelOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_010() {
		Logger.start(true, "获取单个订单信息成功-买手取消");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create13_SellerCancelOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_011() {
		Logger.start(true, "获取单个订单信息成功-买手接单");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create17_AcceptOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_013() {
		Logger.start(true, "获取单个订单信息成功-系统取消");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create18_SystemCancelOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetOrderTradeflow_014() {
		Logger.start(true, "获取单个订单信息成功-已付款 使用宝付支付");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			int thirdTradingId = new OrderCallService().orderPayRequest(userId,
					BigDecimal.ZERO, tradingId, "13");

			PaymentProxyCallService.notifyCall("70",
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);

			SyncVerifyService.getSyncResult(orderId);

			getordertradeflowBean.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(getordertradeflowBean,
					true, false);
			getordertradeflowCall.setData(param);
			getordertradeflowCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getordertradeflowCall.getCode(),
					"验证code");
			Logger.verifyEquals(0, getordertradeflowCall.getBCode(), "验证bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}